﻿using Grpc.Core;
using GRPCClass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SdtpsRPCSDK
{
    /// <summary>
    /// grpc的客户端SDK
    /// 封装grpc的底层访问
    /// </summary>
    public class GRPCClient
    {
        public string Servadd
        {
            get;
            set;
        }
        gRPC.gRPCClient client;
        private bool isopen = false;
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            Channel channel = new Channel(Servadd, ChannelCredentials.Insecure);
            client = new gRPC.gRPCClient(channel);
            isopen = true;
            return true;
        }
        /// <summary>
        /// 测试用
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskReply SayHello(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.SayHello(request);
            return mytr;
        }
        /// <summary>
        /// 添加一个任务 用于任务发起方提交一个处理任务
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskReply AddTask(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.AddTask(request);
            return mytr;
        }
        /// <summary>
        /// 获取没有完成的任务数量
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskReply GetTaskNoEndCount(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.GetTaskNoEndCount(request);
            return mytr;
        }
        /// <summary>
        /// 获取没有分配的任务数量
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskReply GetTaskNoAllotCount(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.GetTaskNoAllotCount(request);
            return mytr;
        }
       /// <summary>
       ///  执行节点 向中心控制节点注册
       /// </summary>
       /// <param name="request"></param>
       /// <returns></returns>
        public TaskReply EnrollNode(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.EnrollNode(request);
            return mytr; 
        }
        /// <summary>
        /// 注销一个执行节点 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskReply LogoutNode(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.LogoutNode(request);
            return mytr;
        }
       /// <summary>
       /// 任务完成后回复
       /// </summary>
       /// <param name="request"></param>
       /// <returns></returns>
        public TaskReply ReplyNode(TaskReply request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.ReplyNode(request);
           
            return mytr;
        }
        /// <summary>
        /// 分配一个任务
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskReply AllotTask(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.AllotTask(request);
            return mytr;
        }
        /// <summary>
        /// 查询任务的情况
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TaskReply QueryTask(TaskRequest request)
        {
            if (!isopen)
            {
                Open();
            }
            TaskReply mytr = client.QueryTask(request);
            return mytr;
        }

    }
}
